<script setup lang="ts">
import CustomNavbar from "./components/CustomNavbar.vue"
import { getHomeBannerAPI, getCategoryAPI, getHomeHotAPI } from "@/services/home"
import { ref } from "vue"
import { onLoad } from "@dcloudio/uni-app";
import type { BannerItem, CategoryItem, HotItem } from "@/types/home"
import type { XtxGuessInstance } from "@/types/component"
import CategoryPanel from "./components/CategoryPanel.vue"
import HotPanel from "./components/HotPanel.vue"
import PageSkeleton from "./components/PageSkeleton.vue";

const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  // console.log(res)
  bannerList.value = res.result
}

const categoryList = ref<CategoryItem[]>([])
const getCategoryData = async () => {
  const res = await getCategoryAPI()
  categoryList.value = res.result
}

const hotList = ref<HotItem[]>([])
const getHotData = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}

const guessRef = ref<XtxGuessInstance>()
const onScolltolower = () => {
  guessRef.value?.getmore()
}

const isTriggered = ref(false)
const onRefresherrefresh = async () => {
  isTriggered.value = true
  // await getHomeBannerAPI()
  // await getCategoryData()
  // await getHotData()
  guessRef.value?.resetData()
  await Promise.all([getHomeBannerData(), getCategoryData(), getHotData(), onScolltolower()])


  isTriggered.value = false
}

const isLoading = ref(false)
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getCategoryData(), getHotData()])
  isLoading.value = false
})

</script>

<template>
  <CustomNavbar></CustomNavbar>
  <scroll-view scroll-y @scrolltolower="onScolltolower" refresher-enabled @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered">
    <PageSkeleton v-if="isLoading"></PageSkeleton>
    <template v-else>
      <XtxSwiper :list="bannerList"></XtxSwiper>
      <CategoryPanel :list="categoryList"></CategoryPanel>
      <HotPanel :list="hotList"></HotPanel>
      <XtxGuess ref="guessRef"></XtxGuess>
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  height: 100%;
  background-color: #f7f7f7;
}
</style>
